package test

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/test"
	testReq "github.com/flipped-aurora/gin-vue-admin/server/model/test/request"
)

type StaffService struct {
}

// CreateStaff 创建staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) CreateStaff(staff *test.Staff) (err error) {
	err = global.GVA_DB.Create(staff).Error
	return err
}

// DeleteStaff 删除staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) DeleteStaff(staff test.Staff) (err error) {
	err = global.GVA_DB.Delete(&staff).Error
	return err
}

// DeleteStaffByIds 批量删除staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) DeleteStaffByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]test.Staff{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateStaff 更新staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) UpdateStaff(staff test.Staff) (err error) {
	err = global.GVA_DB.Save(&staff).Error
	return err
}

// GetStaff 根据id获取staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) GetStaff(id uint) (staff test.Staff, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&staff).Error
	return
}

// GetStaffInfoList 分页获取staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) GetStaffInfoList(info testReq.StaffSearch) (list []test.Staff, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&test.Staff{})
	var staffs []test.Staff
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	//根据工号查询
	if info.ID != 0 {
		fmt.Println(info.ID)
		db = db.Where("id=?", info.ID)
		err = db.Count(&total).Error
		if err != nil {
			return
		}

		if limit != 0 {
			db = db.Limit(limit).Offset(offset)
		}

		err = db.Find(&staffs).Error
		return staffs, total, err
	}
	//根据姓名查询
	if info.Username != "" {
		db = db.Where("username like ?", "%"+info.Username+"%")
	}
	//根据性别查询
	if info.Sex != nil {
		sexBool := *info.Sex
		sexInfo := 0
		if sexBool {
			sexInfo = 1
		}
		db = db.Where("sex=?", sexInfo)
	}
	//根据职位查询
	if info.Job != "" {
		db = db.Where("job like ?", "%"+info.Job+"%")
	}
	//根据部门查询
	if info.Department != "" {
		db = db.Where("department like ?", "%"+info.Department+"%")
	}
	//根据进出类型查询
	if info.Outinstatus != nil {
		sexBool := *info.Outinstatus
		sexInfo := 0
		if sexBool {
			sexInfo = 1
		}
		db = db.Where("outinstatus = ?", sexInfo)
	}
	//根据考勤状态查询
	if info.Examstatus != nil {
		sexBool := *info.Examstatus
		sexInfo := 0
		if sexBool {
			sexInfo = 1
		}
		db = db.Where("examstatus =?", sexInfo)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&staffs).Error
	return staffs, total, err
}
